package settings;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.dwin.navy.serialportapi.com_track;
import com.dwin.navy.serialportapi.com_zhongji;
import com.google.gson.Gson;
import com.imstlife.vending.R;
import com.imstlife.vending.activity.MyApplication;
import com.imstlife.vending.adapter.GvBaseAdapter;
import com.imstlife.vending.utils.Cof;
import com.imstlife.vending.utils.SharePersistentUtil;
import com.imstlife.vending.utils.T;
import com.imstlife.vending.vo.DrinkListVo;
import com.imstlife.vending.vo.GetDrinkListResponse;

public class CheckOutTradeFragment extends Fragment implements GvBaseAdapter.GVInter {
    private DrinkListVo drinkListVo;
    /**
     * 以勒串口API
     */
    private com_track yileSerial;

    /**
     * 中吉串口API
     */
    private com_zhongji zhongjiSerial;
    private String deviceModel;

    private boolean flag = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootLayout = inflater.inflate(R.layout.fragment_checkouttrade,
                container, false);
        deviceModel = getActivity().getIntent().getStringExtra("deviceModel");
        drinkListVo = ((DrinkListVo) getActivity().getIntent().getSerializableExtra("drinks"));
        GridView gv = (GridView) rootLayout.findViewById(R.id.gv);
        gv.setAdapter(new GvBaseAdapter(getActivity(),drinkListVo.getDrinkList().size(),this));
        return rootLayout;
    }

    @Override
    public void BtnOnClick(int num) {
        if (!flag){
            T.showShort2(getActivity(),"开始检测货道"+num);
            out(num);
        }
    }
    public void out(final int dao){

        flag=true;
        new Thread() {
            @Override
            public void run() {
                String retstr = null;
                try {
                    int idao = dao;
                    if(Cof.MODEL_01.equals(deviceModel)){
                        retstr = yileSerial.chu_huo_track(idao);
                    }else if(Cof.MODEL_02.equals(deviceModel)||Cof.MODEL_03.equals(deviceModel)||Cof.MODEL_04.equals(deviceModel)){
                        retstr = zhongjiSerial.chu_huo_track(dao,deviceModel);
                    }

                } catch (Exception e) {
                    retstr = "Exception: " + e.getMessage();
                }

                final String hui = retstr;
                Log.d("out_return:","出货结果"+hui);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (null==hui||hui.length() == 0) {
                            flag = false;
                            T.showShort2(MyApplication.getInstance(), "出货成功");
                        } else {
                            flag = false;
                            T.showShort2(MyApplication.getInstance(), hui);
                        }
                    }
                });
            }
        }.start();
    }

    @Override
    public void onResume() {
        super.onResume();
        flag = false;
        String str = SharePersistentUtil.getSharePersistentString(getActivity(),"spnum");
        int iserial = Cof.SERIAL_PORT_NUM;
        if (str!=null&&!str.equals("")){
            iserial = Integer.parseInt(str);
        }
        if(Cof.MODEL_01.equals(deviceModel)){
            yileSerial = new com_track(iserial);
            yileSerial.openSerialPort();
        }else if(Cof.MODEL_02.equals(deviceModel)||Cof.MODEL_03.equals(deviceModel)||Cof.MODEL_04.equals(deviceModel)){
            zhongjiSerial  = com_zhongji.getInstance(iserial);
            zhongjiSerial.openSerialPort();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(Cof.MODEL_01.equals(deviceModel)){
            if(yileSerial!=null){
                yileSerial.closeSerialPort();
            }
        }else if(Cof.MODEL_02.equals(deviceModel)||Cof.MODEL_03.equals(deviceModel)||Cof.MODEL_04.equals(deviceModel)){
            if(zhongjiSerial!=null){
                zhongjiSerial.closeSerialPort();
            }
        }
    }
}
